package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.LocationService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.entity.driver.DriverSet;
import com.atguigu.daijia.model.form.map.OrderServiceLocationForm;
import com.atguigu.daijia.model.form.map.UpdateDriverLocationForm;
import com.atguigu.daijia.model.form.map.UpdateOrderLocationForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class LocationServiceImpl implements LocationService {

    @Autowired
    private LocationFeignClient locationFeignClient;

    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;


    //司机上线：更新司机的位置信息到GEO中
    @Override
    public Boolean updateDriverLocation(UpdateDriverLocationForm locationForm) {
        //获取司机的个性化信息
        Result<DriverSet> result = driverInfoFeignClient.getDriverSet(locationForm.getDriverId());
        //判断司机是否开启了接单
        if (result.getData().getServiceStatus()==1){
            Result<Boolean> booleanResult = locationFeignClient.updateDriverLocation(locationForm);
            return booleanResult.getData();
        }
        else
            //未开启接单
            throw new GuiguException(ResultCodeEnum.NO_START_SERVICE);
    }

    //司机赶往代驾起始点：更新司机位置到Redis缓存
    @Override
    public Boolean updateOrderLocationToCache(UpdateOrderLocationForm updateOrderLocationForm) {
        Boolean b = locationFeignClient.updateOrderLocationToCache(updateOrderLocationForm).getData();
        return b;
    }

    //开始代驾服务：批量保存代驾服务订单位置
    @Override
    public Boolean saveOrderServiceLocation(List<OrderServiceLocationForm> orderLocationServiceFormList) {
        Boolean b = locationFeignClient.saveOrderServiceLocation(orderLocationServiceFormList).getData();
        return b;
    }
}
